package uiadds;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView.LayoutParams;

public class ThrusterSlider extends View{
	Paint mPaint;

	int height = 200;
	int width = 10;
	Progress prog;
	float oldYPos;
	
	public ThrusterSlider(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
		prog = new Progress( new Point(width/2, height), height);
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		int r = prog.getRadius();
		mPaint.setColor(Color.GRAY);
		canvas.drawRect(r/2, 0, r*3/2, height - r, mPaint);
		mPaint.setColor(Color.GREEN);
		canvas.drawRect(r/2,  prog.getProgress().y, r*3/2, height - r, mPaint);
		mPaint.setColor(Color.RED);
		canvas.drawCircle(width/2, prog.getProgress().y, r, mPaint);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
	    if(getLayoutParams().width==LayoutParams.WRAP_CONTENT)
	    {
	        width = 2*prog.getRadius();
	    }
	    else if((getLayoutParams().width==LayoutParams.MATCH_PARENT)||(getLayoutParams().width==LayoutParams.FILL_PARENT))
	    {
	        width = MeasureSpec.getSize(widthMeasureSpec);
	    }
	    else
	        width = getLayoutParams().width;


	    if(getLayoutParams().height==LayoutParams.WRAP_CONTENT)
	    {
	    	height = 200 + prog.getRadius();
	    }
	    else if((getLayoutParams().height==LayoutParams.MATCH_PARENT)||(getLayoutParams().height==LayoutParams.FILL_PARENT))
	    {
	        height = MeasureSpec.getSize(heightMeasureSpec);
	    }
	    else
	        height = getLayoutParams().height;

	    setMeasuredDimension(width|MeasureSpec.EXACTLY, height|MeasureSpec.EXACTLY);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		float xpos = event.getX();
		float ypos = event.getY();
		Point p = prog.getProgress();
		
		if(event.getAction() == MotionEvent.ACTION_DOWN)
			oldYPos = event.getY();
		else if((xpos <= p.x + prog.getRadius() && xpos >= p.x - prog.getRadius())
			&&(ypos <= p.y + prog.getRadius() && ypos >= p.y - prog.getRadius()))
		{
			if(event.getAction() == MotionEvent.ACTION_MOVE)
			{
				prog.updateProgress((int)(ypos-oldYPos));
				oldYPos = ypos;
				invalidate();
			}
		}
		return true;
	}	
	
	public double getProgress()
	{
		return (prog.getProgress().y - prog.getStartPoint().y)/prog.getLimit();
	}
	
	public class Progress
	{
		private Point startPoint;
		private Point progress;
		private int radius = 15;
		private float limit;
		
		public Progress(Point startPoint, float limit)
		{
			this.startPoint = startPoint;
			this.limit = limit;
			progress = new Point(startPoint.x, startPoint.y);
		}
		
		public Point getProgress()
		{
			return progress;
		}
		
		public Point getStartPoint()
		{
			return startPoint;
		}
		
		public void updateProgress(int dy)
		{
			if((dy > 0 && progress.y < limit) || (dy < 0 && progress.y > radius))
				progress.set(progress.x, progress.y + dy);
		}
		
		public float getLimit()
		{
			return limit;
		}
		
		public int getRadius()
		{
			return radius;
		}
	}

}
